#pragma once

#include "const.h"
#include "LogicSystem.h"

class HttpConnection : public std::enable_shared_from_this<HttpConnection>
{
	friend class LogicSystem;

public:
	HttpConnection(boost::asio::io_context& ioc);

	// 监听读写事件
	void start();

	// 获取socket
	boost::asio::ip::tcp::socket& getSocket() {
		return m_httpSocket;
	}

private:
	// 超时检测
	void checkDeadline();
	// 处理请求
	void handleReq();
	// 返回的应答
	void writeResponse();
	// 解析url
	void preParseGetParam();

private:
	boost::asio::ip::tcp::socket m_httpSocket;
	boost::beast::flat_buffer m_buffer{ 8192 }; // 8k 接受数据的缓冲区
	boost::beast::http::request<boost::beast::http::dynamic_body> m_request;	// 接受http请求
	boost::beast::http::response<boost::beast::http::dynamic_body> m_response;  // 回复http请求
	boost::asio::steady_timer m_deadline{ m_httpSocket.get_executor(), std::chrono::seconds(60) }; // 定时器（默认设置为60s）
	std::string m_getUrl; // url
	std::unordered_map<std::string, std::string> m_getParams; // http的key与value的映射
};

// 从16进制转为十进制
unsigned char FromHex(unsigned char x);

// char 转为16进制
// char 转为16进制
unsigned char ToHex(unsigned char x);

// url编码
std::string UrlEncode(const std::string& str);

// url解码
std::string UrlDecode(const std::string& str);
